<?php

namespace App\Model;

use App\Exceptions\SmsValidateException;
use Illuminate\Database\Eloquent\Model;

/**
 * Class SmsMsg
 *
 * @package App\Model
 * @method static \Illuminate\Database\Query\Builder|SmsMsg where($column, $operator = null, $value = null, $boolean = 'and')
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg query()
 * @property int $id 短信验证码表
 * @property int $type 验证码类型0其他1登录2注册3改密4换绑5设置支付密码
 * @property string $mobile_phone 手机号码
 * @property string $dead_line 过期时间
 * @property string $msg_code 短信验证码
 * @property int $is_valided 是否已经使用(0:未使用,1:已使用)
 * @property int $is_sended 是否已经发送(0:未发送,1:已发送)
 * @property string $temp_id 模板id
 * @property string $data 发送数据
 * @property string|null $result_info 返回结果
 * @property \Illuminate\Support\Carbon $created_at 创建日期
 * @property \Illuminate\Support\Carbon $updated_at 修改时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereData($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereDeadLine($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereIsSended($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereIsValided($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereMobilePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereMsgCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereResultInfo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereTempId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SmsMsg whereUpdatedAt($value)
 */
class SmsMsg extends Model
{
    protected $table = 'sms_validate';

    protected $primaryKey = 'id';

    public function add($mobile_phone, $msg_code)
    {
        $this->mobile_phone = $mobile_phone;
        $this->msg_code = $msg_code;
        $this->dead_line = time();
        return $this->save();
    }

    /**
     * @param string $phone 手机号
     * @param string $code 验证码
     * @param int $type 验证类型
     * @throws SmsValidateException
     */
    public function validate($phone, $code, $type = 0)
    {
        $dataTime = date('Y-m-d H:i:s', time());
        $model = $this->where('mobile_phone', $phone);
        $model->where('is_valided', 0);
        $model->where('is_sended', 1);
        $model->where('msg_code', $code);
        if ($type != 0){
            $model->where('type', $type);
        }
        $m = $model->first();

        if ($m == null or $m->dead_line < $dataTime) {
            throw new SmsValidateException();
        }
        $m->is_valided = 1;
        $m->save();
    }

}
